package com.achilles.widget;

import android.content.Context;
import android.text.method.TextKeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.achilles.bean.DataBean;
import com.achilles.bean.GiftBean;
import com.achilles.playtask.R;


/**
 * Created by AchillesL on 2016-9-21.
 */
public class CreateGiftView extends DialogView {

    private EditText mEdGiftName;
    private EditText mEdGiftScore;
    private RadioGroup mRgGiftType;
    private GiftBean.GiftType mGiftType;
    private GiftBean mGiftEditBean;
    private boolean mIsEditMode = false;

    public CreateGiftView(Context context) {
        super(context);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.gift_dialog_view;
    }

    @Override
    protected void initView(View contentView) {
        mEdGiftName = (EditText) mContentView.findViewById(R.id.edGiftName);
        mEdGiftScore = (EditText) mContentView.findViewById(R.id.edGiftScore);
        mRgGiftType = (RadioGroup) mContentView.findViewById(R.id.rgGiftType);

        mGiftType = GiftBean.GiftType.NoLimit;
        mRgGiftType.check(R.id.rbNoLimit);
        mRgGiftType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbSingle) {
                    mGiftType = GiftBean.GiftType.Single;
                } else if (checkedId == R.id.rbNoLimit) {
                    mGiftType = GiftBean.GiftType.NoLimit;
                }
            }
        });
    }

    @Override
    public void editData(DataBean dataBean) {
        mIsEditMode = true;
        mGiftEditBean = (GiftBean) dataBean;

        String giftName = mGiftEditBean.getGiftName();
        mEdGiftName.setText(giftName);
        mEdGiftName.setSelection(giftName.length());

        String giftScore = String.valueOf(mGiftEditBean.getGiftScore());
        mEdGiftScore.setText(giftScore);
        mEdGiftScore.setSelection(giftScore.length());

        GiftBean.GiftType giftType = ((GiftBean) dataBean).getGiftType();
        if (giftType == GiftBean.GiftType.Single) {
            mRgGiftType.check(R.id.rbSingle);
            mGiftType = GiftBean.GiftType.Single;
        }else if (giftType == GiftBean.GiftType.NoLimit) {
            mRgGiftType.check(R.id.rbNoLimit);
            mGiftType = GiftBean.GiftType.NoLimit;
        }
    }

    @Override
    public DataBean getData() {
        GiftBean giftBean;
        if (mIsEditMode) {
            giftBean = mGiftEditBean;
        }else {
            giftBean = new GiftBean();
            giftBean.setGiftCreateTime(String.valueOf(System.currentTimeMillis()));
        }

        String giftName = mEdGiftName.getText().toString();
        int giftScore = Integer.parseInt(mEdGiftScore.getText().toString());
        giftBean.setGiftName(giftName);
        giftBean.setGiftScore(giftScore);
        giftBean.setGiftType(mGiftType);
        giftBean.setCompleted(false);
        giftBean.setGiftOrder(mGiftType.ordinal());

        Log.d("getData",giftBean.toString());
        return giftBean;
    }

    @Override
    public boolean isParamValid() {
        if (mEdGiftName.length() == 0) {
            mEdGiftName.requestFocus();
            mEdGiftName.setError(getView().getContext().getString(R.string.create_error_name_empty));
            return false;
        }
        if (mEdGiftScore.length() == 0) {
            mEdGiftScore.requestFocus();
            mEdGiftScore.setError(getView().getContext().getString(R.string.create_error_score_empty));
            return false;
        }
        return true;
    }

    @Override
    public void reset() {
        TextKeyListener.clear(mEdGiftName.getText());
        TextKeyListener.clear(mEdGiftScore.getText());
        mEdGiftName.requestFocus();
        mRgGiftType.check(R.id.rbNoLimit);
        mGiftType = GiftBean.GiftType.NoLimit;
        mIsEditMode = false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        return false;
    }
}
